<?php

namespace MUD;

class Handler{
    public function onConnect(){
        R::current()->client->send("\t\tWelcome to College\n\tDesigned and coded by Phil Davies\n\t    Copyright Phil Davies 2013\n\n");
        
        do{
            do{
                R::current()->send("To begin, type LOGIN or REGISTER:", "\t", false);
                
                $option = R::current()->read();
                
            }while($option == '');
            
            if($option == 'r' || $option == 'register'){
                //register new account
                R::current()->client->register();
            }else{
                //login to existing account
                R::current()->client->login();
            }
        }while(false === R::current()->client->authenticated);
        
        R::current()->client->send("Welcome, ".R::current()->client->username."!", "\n\n");
        
        R::current()->map->setCurrentRoom(1);
        R::current()->map->printRoom();
    }

    public function onReceiveData($m=''){
        if(empty($m)){
            R::current()->send("", "\n", true);
        }else{
            if(substr_count($m, ' ') < 1){
                switch($m){
                    case 'n':case's':case'e':case'w':case'north':case'south':case'east':case'west':
                        R::current()->map->walk($m);
                    break;
                    case 'ex' : 
                        R::current()->map->room->showExits();
                    break;
                    case 'shit':
                        \MUD\R::current()->send("A crow flaps overhead, spraying you with white droppings.", "\n", true);
                    break;
                    case 'fuck':
                        \MUD\R::current()->send("Well, aren't we mature...", "\n", true);
                    break;
                    default:
                        \MUD\R::current()->send("I diddn't understand that.", "\n", true);
                    break;
                }
            }else{
                $ex = explode(' ', $m);
                $cmd = strtolower($ex[0]);
                array_shift($ex);
                $msg = implode(' ', $ex);
                
                switch($cmd){
                    case 'shout' :
                        R::current()->client->sendBroadcast(R::current()->client->username." shouts ".$msg);
                    break;
                    case 'tell' :
                        $msg=explode(' ', $msg);
                        $target_user = $msg[0];
                        array_shift($msg);
                        $msg=implode(' ', $msg);
                        
                        if(false === R::current()->client->whisper($msg, $target_user)){
                            R::current()->client->send("There not around i'm affraid.");
                        }
                    break;
                    default:
                        R::current()->client->send("I diddn't understand that.", "\n", true);
                    break;
                }
            }
        }
    }
    
    public function onQuit(){
        \MUD\R::current()->send("Thanks for playing!\nGoodbye.", "\n\n", false);
        R::current()->dbo->delete("clientracker", "pid='".getmypid()."'");
        R::current()->dbo->update("users", array('pid' => '0'), "id='".R::current()->client->user_id."'", 1);
        $this->onDisconnect();
    }
    
    public function onDisconnect(){
        R::current()->dbo->close();
    }
}